package level.editor.control.object;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.WindowFocusListener;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import level.editor.control.Control;
import level.editor.control.main.MainTabbed;
import level.editor.view.Window;
import level.editor.view.action.ActionConstants;
import level.model.GameObject;
import level.model.utility.GameObjectFactory;

public class ObjectControl extends Control
{

	public ObjectControl(MainTabbed tabbed)
	{
		super(tabbed);
		init();
	}

	private void init()
	{
		setName("Object Tool");
		setIcon(new ImageIcon(ActionConstants.ICON_DIR + "object.png"));
		setMinimumSize(new Dimension(200, 100));
		initHandler();
		initObjectSelect();
		initOptions();
	}

	private void initHandler()
	{
		handler = new ObjectHandler(this);
		((Window) tabbed.getTopLevelAncestor())
				.addWindowFocusListener((WindowFocusListener) handler);
	}

	private void initObjectSelect()
	{
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING, 10, 10));
		panel.setBorder(BorderFactory.createTitledBorder("Objects"));
		panel.add(new ObjectSelect(GameObjectFactory.createGoomba(0, 0)));
		panel.add(new ObjectSelect(GameObjectFactory.createCrate(0, 0)));
		panel.add(new ObjectSelect(GameObjectFactory.createStartPosition(0, 0)));
		add(panel, BorderLayout.CENTER);
	}

	private void initOptions()
	{
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createTitledBorder("Options"));
		panel.add(new JButton("Placeholder"));
		add(panel, BorderLayout.SOUTH);
	}

	public class ObjectSelect extends JButton
	{
		public ObjectSelect(GameObject o)
		{
			super();
			addActionListener((ActionListener) handler);
			obj = o;
			icon = new ImageIcon(obj.getIconPath());
			this.setIcon(icon);
		}
		
		public GameObject getObject()
		{
			return obj;
		}
		
		public Icon getGameIcon()
		{
			return icon;
		}

		private GameObject obj;
		private Icon icon;
	}
}
